﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Menus.Model.DB;

using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;


namespace Menus.Model
{
    public class IngredientManager
    {
        //chargement d'un ingrédient
        public Ingredient load(string libelle)
        {
            Ingredient ingredient = Globals.menusEF.Ingredient.Where(l => l.Libelle == libelle).First();
            return ingredient;
        }

        //chargement d'un ingredient, par ID
        public Ingredient load(int id)
        {
            Ingredient ingredient = Globals.menusEF.Ingredient.Where(i => i.ID == id).First();
            return ingredient;
        }

        //ajout d'un ingrédient
        public Ingredient Add(string libelle, int cout, int idType = -1) 
        {
            //on n'ajoute pas l'ingredient s'il existe déjà
            Ingredient ing = Globals.menusEF.Ingredient.Where(i => i.Libelle == libelle).SingleOrDefault();
            if (ing != null)
                return ing;

            //récupération du type d'ingrédient
            IngredientType ingredientType;

            //si le type de l'ingrédient est indéfini (-1) on sélectionne le type 'autre'
            if (idType == -1)
                ingredientType = Globals.menusEF.IngredientType.Where(it => it.Libelle == "Autre").First();
            else
                ingredientType = Globals.menusEF.IngredientType.Where(it => it.Id == idType).First();
                       
            Ingredient ingredient = new Ingredient();
            ingredient.IngredientType = ingredientType;
            ingredient.Libelle = libelle.ToLower();
            ingredient.Cout = cout;

            Globals.menusEF.Ingredient.Add(ingredient);
            Globals.menusEF.SaveChanges();
            return ingredient;
        }

        //mise à jour de l'ingredient
        public void Update(int id, string libelle, int cout)
        {
            var modifyIngredient = Globals.menusEF.Ingredient.Where(i => i.ID == id).First();
            modifyIngredient.Libelle = libelle;
            modifyIngredient.Cout = cout;
            Globals.menusEF.SaveChanges();
        }

        //suppression d'un ingrédient
        public void delete(string libelle)
        { 
            Ingredient ingredient = Globals.menusEF.Ingredient.Where(l => l.Libelle == libelle).First();


            Globals.menusEF.Ingredient.Remove(ingredient);
            Globals.menusEF.SaveChanges();        
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void delete(int idIngredient)
        {
            //sélection des plats associés à cet ingrédient
            //il faut supprimer les infos de la table d'associations entre plat et ingrédient
            var plats = from p in Globals.menusEF.Plat
                        from i in p.Ingredient
                        where i.ID == idIngredient
                        select new { p.ID };

            //sélection de l'ingredient
            var ingredient = Globals.menusEF.Ingredient.Where(i => i.ID == idIngredient).First();

            //on parcours les plats
            foreach (var platid in plats)
            {
                //sélection du plat qui possède cet ingrédient
                var plat = Globals.menusEF.Plat.Where(p => p.ID == platid.ID).First();
                
                //suppression de l'association
                plat.Ingredient.Remove(ingredient);                
            }

            //sélection de l'ingredient
            Globals.menusEF.Ingredient.Remove(ingredient);
            Globals.menusEF.SaveChanges();        
        }
         
        //retourne true si l'ingredient existe
        public bool Exists(string libelle)
        {
            return Globals.menusEF.Ingredient.Where(l => l.Libelle == libelle).Count() > 0;            
        }

        /// <summary>
        /// retourne le cout de l'ingredient
        /// </summary>
        /// <param name="libelle"></param>
        /// <returns></returns>
        public int GetCout(string libelle)
        {
            var cout = (from i in Globals.menusEF.Ingredient
                        where i.Libelle == libelle
                        select i.Cout).SingleOrDefault();
            if (cout == 0)
                cout = Globals.coutNormal;
            return cout;
        }

        public List<string> LoadListIngredient()
        {
            List<string> listIngredient = new List<string>();
            var qry = from ingredient in Globals.menusEF.Ingredient
                      select ingredient;

            qry.ToList().ForEach(i => listIngredient.Add(i.Libelle));

            return listIngredient;

        
        }

        /// <summary>GetListIngredient()
        /// retourne la liste des ingredient à afficher dans la GridView
        /// </summary>
        /// <returns></returns>
        public BindingSource GetListIngredient()
        {
            //var getData = Globals.menusEF.Ingredient.ToList();
            var getData = from i in Globals.menusEF.Ingredient
                          from t in Globals.menusEF.IngredientType
                          where i.IngredientType.Id == t.Id
                          select new { i.ID, ingredient = i.Libelle, type = t.Libelle  };

                
            BindingSource source = new BindingSource();
            source.DataSource = getData.ToList();
            return source;  
 

        }

        /// <summary>GetListPlat
        /// retourne la liste des plats qui utilisent l'ingredient idIngredient
        /// </summary>
        /// <param name="idIngredient"></param>
        /// <returns></returns>
        public List<string> GetListPlat(int idIngredient)
        {
            var listPlat = from p in Globals.menusEF.Plat
                           where p.Ingredient.Select(i => i.ID).Contains(idIngredient)
                           select p;
                           

            List<string> returnList = new List<string>();

            foreach(var item in listPlat) 
            {
                returnList.Add(item.Libelle);               
            }
           
            return returnList;
        
        }


    }
}
